//
// Created by root on 6/22/17.
//

#include "statusbar.h"

void
status_save_filename(FunnyWindow *app, gchar *filename) {   /* filename parameter not used in this implementation */
    gchar *status = g_strdup_printf("Saving %s...", app->filename);
    status_update_str(app, status);
    g_free(status);
}

void status_update_str(FunnyWindow *app, gchar *s) {
    if (app->cid)               /* pop previous statusbar entry */
        gtk_statusbar_pop(GTK_STATUSBAR (app->statusbar), app->cid);

    gchar *str;
    if (s && *s)
        str = g_strdup(s);
    else
        str = g_strdup_printf("Error : statusbar update failed 'NULL'");

    app->cid = gtk_statusbar_get_context_id(GTK_STATUSBAR (app->statusbar), str);
    gtk_statusbar_push(GTK_STATUSBAR (app->statusbar), app->cid, str);

    g_free(str);
}

void status_pop(GtkWidget *widget, FunnyWindow *app) {
    if (app->cid)
        gtk_statusbar_pop(GTK_STATUSBAR (app->statusbar), app->cid);
}

void status_set_default(FunnyWindow *app) {
    gchar *status = g_strdup_printf(" line:%5d :%4d  |  %s",
                             app->line + 1, app->col + 1,
                             app->overwrite ? "OVR" : "INS");

    if (app->cid)               /* pop previous statusbar entry */
        gtk_statusbar_pop(GTK_STATUSBAR (app->statusbar), app->cid);
    else
        app->cid = gtk_statusbar_get_context_id(GTK_STATUSBAR (app->statusbar),
                                                status);
    gtk_statusbar_push(GTK_STATUSBAR (app->statusbar), app->cid, status);

    g_free(status);
}
